package com.shuai.edit;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.shuai.adapter.MemoryEditAdapter;
import com.shuai.sql.SQL;
import com.shuai.stickynote.EditActivity;
import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.R;

import java.util.ArrayList;

public class MemoryEditActivity extends AppCompatActivity {
    private Button btn_submit;
    private ListView lv_list;
    ImageView btn_back, btn_delete;
    private MemoryEditAdapter adapter;
    private SQL sql;
    private String group = "";
    private ArrayList<MemoryNote> memorys;
    private String TAG = "fuck";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_edit);
        init();
    }

    private void init() {
        lv_list = findViewById(R.id.memory_edit_list);
        btn_submit = findViewById(R.id.memory_edit_submit);
        btn_back = findViewById(R.id.topbar_btn_back);
        btn_delete = findViewById(R.id.topbar_edit_delete);
        sql = EditActivity.sql;
        getGroupData();
        adapter = new MemoryEditAdapter(this);
        if (!group.equals("")){
            memorys = sql.selectMemoryByGroup(group);
            adapter.addMemory(memorys);
        }
        adapter.addNewNote();
        lv_list.setAdapter(adapter);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (group.equals("")) adapter.deleteAll();
                else deleteMemory();
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void deleteMemory() {
        new AlertDialog.Builder(this)
                .setTitle("警告")
                .setMessage("确定删除这一组单词吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sql.deleteTaskByGroup(group);
                        finish();
                    }
                })
                .setNegativeButton("取消",null)
                .create().show();
    }

    private void saveData() {
        if (!adapter.sortData()) Toast.makeText(this, "没有数据噢~", Toast.LENGTH_SHORT).show();
        else {
            ArrayList<MemoryNote> memory_update = adapter.getMemory_update();
            ArrayList<MemoryNote> memory_new = adapter.getMemory_new();
            ArrayList<String> memory_delete = adapter.getMemory_delete();
            if (memory_update.size() > 0) sql.updateMemory(memory_update);
            if (memory_new.size() > 0) sql.insertMemory(memory_new);
            if (memory_delete.size() > 0) sql.deleteMemory(memory_delete);
            Toast.makeText(this, "已保存~", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void getGroupData(){
        Intent intent = getIntent();
        group = intent.getStringExtra("group");
    }

}